/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.form.editor.model.document.impl;

import java.io.File;
import java.rmi.server.UID;

import com.genia.toolbox.basics.editor.model.document.Document;
import com.genia.toolbox.form.editor.model.form.impl.FormModel;

/**
 * The document model.
 */
public class DocumentModel
    implements Document<FormModel>
{

  /**
   * The document name.
   */
  private String documentName = null;

  /**
   * Whether the document is saved, and has not been modified.
   */
  private boolean isSaved = false;

  /**
   * The unique ID.
   */
  private UID uniqueId = null;

  /**
   * The XML file.
   */
  private File xml = null;

  /**
   * The root form model.
   */
  private FormModel formModel = null;

  /**
   * Whether the document is a prototype.
   */
  private boolean isPrototype = false;



  /**
   * Constructor.
   * 
   * @param name
   *          the document name.
   * @param xml
   *          the xml file.
   */
  public DocumentModel(String name, File xml)
  {
    this(name);
    this.xml = xml;
  }



  /**
   * Constructor.
   * 
   * @param name
   *          the document name.
   */
  public DocumentModel(String name)
  {
    super();
    this.uniqueId = new UID();
    this.isSaved = false;
    this.documentName = name;
  }



  /**
   * Constructor.
   * 
   * @param name
   *          the document name.
   * @param xml
   *          the xml file.
   * @param formModel
   *          the form model.
   */
  public DocumentModel(String name, File xml, FormModel formModel)
  {
    this(name, formModel);
    this.xml = xml;
  }



  /**
   * Constructor.
   * 
   * @param name
   *          the document name.
   * @param formModel
   *          the form model.
   */
  public DocumentModel(String name, FormModel formModel)
  {
    super();
    this.uniqueId = new UID();
    this.formModel = formModel;
    this.formModel.setDocumentID(this.uniqueId);
    this.isSaved = false;
    this.documentName = name;
  }



  /**
   * Check whether the settings are valid.
   * 
   * @return whether the settings are valid.
   */
  public boolean accept()
  {
    boolean isOK = true;
    if (this.getName() == null || this.getName().trim().length() == 0) {
      isOK = false;
    }
    return isOK;
  }



  /**
   * Get the document name.
   * 
   * @return the document name.
   */
  public String getName()
  {
    return this.documentName;
  }



  /**
   * The unique ID.
   * 
   * @return the unique ID.
   */
  public UID getUniqueId()
  {
    return this.uniqueId;
  }



  /**
   * Get whether the document is saved and has not been modified.
   * 
   * @return whether the document is saved and has not been modified.
   */
  public boolean isSaved()
  {
    if (this.xml == null) {
      this.isSaved = false;
    }
    return this.isSaved;
  }



  /**
   * Set whether the document is saved and has not been modified.
   * 
   * @param saved
   *          whether the document is saved and has not been modified.
   */
  public void setIsSaved(boolean saved)
  {
    this.isSaved = saved;
  }



  /**
   * Get whether the document is prototype and has not been modified.
   * 
   * @return whether the document is prototype and has not been modified.
   */
  public boolean isPrototype()
  {
    return this.isPrototype;
  }



  /**
   * Set whether the document is prototype and has not been modified.
   * 
   * @param isPrototype
   *          whether the document is a prototype and has not been modified.
   */
  public void setIsPrototype(boolean isPrototype)
  {
    this.isPrototype = isPrototype;
  }



  /**
   * Set the document name.
   * 
   * @param name
   *          the document name.
   */
  public void setName(String name)
  {
    this.documentName = name;
  }



  /**
   * Set the xml file.
   * 
   * @param xml
   *          the xml file.
   */
  public void setXml(File xml)
  {
    this.xml = xml;
  }



  /**
   * Get the xml file.
   * 
   * @return the xml file.
   */
  public File getXml()
  {
    return this.xml;
  }



  /**
   * Get the root form model.
   * 
   * @return the root form model.
   */
  public FormModel getElementModel()
  {
    return this.formModel;
  }



  /**
   * Set the root form model.
   * 
   * @param form
   *          the root form model.
   */
  public void setElementModel(FormModel form)
  {
    this.formModel = form;
    this.formModel.setDocumentID(this.getUniqueId());
  }



  /**
   * Update the specified form.
   * 
   * @param updatedForm
   *          the update form model.
   */
  public void updateElement(FormModel updatedForm)
  {
    if (this.formModel.getUniqueID().equals(updatedForm.getUniqueID())) {
      this.formModel = updatedForm;
    }
    else {
      this.formModel.updateForm(updatedForm);
    }
  }

}
